package com.sunsharing.dispatch.graph;

import com.sunsharing.common.config.NodeModel;
import com.sunsharing.common.config.RouterModel;
import com.sunsharing.common.register.Register;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-4-13
 * Time: 下午4:05
 * To change this template use File | Settings | File Templates.
 */
public class Graph {
    //节点列表
    private Vertex vertexList[];
    //连接矩阵
    private int adjMat[][];

    private int nVerts;
    //n个点
    private  int MAX_VERTS = 5;


    int i = 0;
    int j = 0;


    public Vertex[] getVertexList() {
        return vertexList;
    }

    public void setVertexList(Vertex[] vertexList) {
        this.vertexList = vertexList;
    }

    public int[][] getAdjMat() {
        return adjMat;
    }

    public void setAdjMat(int[][] adjMat) {
        this.adjMat = adjMat;
    }

    public int getN(){

        return MAX_VERTS;

    }

    Map<String,Integer> nodeId2Index = new HashMap<String, Integer>();

    public Graph(Register register)
    {
        List<NodeModel> nodes = register.getNodes();
        MAX_VERTS = nodes.size();
        //邻接矩阵
        adjMat = new int[MAX_VERTS][MAX_VERTS];
        //顶点数组
        vertexList = new Vertex[MAX_VERTS];
        nVerts = 0;

        for(i =0; i<MAX_VERTS; i++)
        {
            for(j=0;j<MAX_VERTS;j++)
            {
                adjMat[i][j] = 0;
            }
        }

//        addVertex('A');
//        addVertex('B');
//        addVertex('C');
//        addVertex('D');
//        addVertex('E');

        for(int i=0;i<MAX_VERTS;i++)
        {
            addVertex(nodes.get(i).nodeId);
            nodeId2Index.put(nodes.get(i).nodeId,i);
        }

        //添加关系
        List<RouterModel> routers = register.getRouters();
        for(RouterModel m:routers)
        {
            int fromIndex = nodeId2Index.get(m.fromNode.nodeId);
            int toIndex = nodeId2Index.get(m.toNode.nodeId);
            addEdge(fromIndex,toIndex);
            if(m.isTwoWay)
            {
                addEdge(toIndex,fromIndex);
            }
        }
    }
    //有向图，添加边
    private void addEdge(int start,int end)
    {
        adjMat[start][end] = 1;
    }

    //添加点
    public void addVertex(String lab)
    {
         vertexList[nVerts++] = new Vertex(lab);
    }

    private void delEdge(int start,int end)
    {
        adjMat[start][end] = 0;
    }

    public String displayVertex(int i)
    {
        return vertexList[i].getLabel();
    }

    public boolean displayVertexVisited(int i)
    {
        return vertexList[i].isWasVisited();
    }

    public void printGraph() {
//        for (i = 0; i < MAX_VERTS; i++) {
//            System.out.print("第" + displayVertex(i) + "个节点:" + " ");
//
//            for (j = 0; j < MAX_VERTS; j++) {
//                System.out.print(displayVertex(i) + "-" + displayVertex(j)
//                        + "：" + adjMat[i][j] + " ");
//            }
//            System.out.println();
//        }

    }

    public Integer getIndexByNodeId(String nodeId)
    {
        return nodeId2Index.get(nodeId);
    }

    public static void main(String[] args) {
        //第几张图，有两张(0,1)，起点序号(0-6)，终点序号(0-6)
//        AF operation = new AF(new Graph(null), 0, 1);
//        operation.getResult();

    }

}
